/**
 * jims
 */
package com.jims.master.modules.csm.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.ChineseCharToEnUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.csm.entity.ExpSupplierCata;
import com.jims.master.modules.csm.service.ExpSupplierCataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 消耗品厂商Controller
 * @author DT
 * @version 2017-03-01
 */
@Controller
@RequestMapping(value = "${adminPath}/csm/expSupplierCata")
public class ExpSupplierCataController extends BaseController {

	@Autowired
	private ExpSupplierCataService expSupplierCataService;

	/**
	 * 根据ID获取并返回对象
	 * @param id
	 * @return
	 */
	@ModelAttribute
	public ExpSupplierCata get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return expSupplierCataService.get(id);
		}
		else {
			return new ExpSupplierCata();
		}
	}

	/**
	 * 获取列表
	 * @param expSupplierCata
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(ExpSupplierCata expSupplierCata, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ExpSupplierCata> page = expSupplierCataService.findPage(new Page<ExpSupplierCata>(request, response), expSupplierCata);
		model.addAttribute("page", page);
		return "modules/csm/expSupplierCataIndex";
	}

	/**
	 * 添加和编辑跳转
	 * @param expSupplierCata
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(ExpSupplierCata expSupplierCata, Model model) {
		model.addAttribute("expSupplierCata", expSupplierCata);
		return "modules/csm/expSupplierCataForm";
	}

	/**
	 * 数据更新与保存
	 * @param expSupplierCata
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(ExpSupplierCata expSupplierCata, Model model, HttpServletRequest request, HttpServletResponse response)throws IOException{
		List<ExpSupplierCata> list=expSupplierCataService.getListBySupplierName(expSupplierCata.getSupplierName(),expSupplierCata.getId());
		int i=0;
		StringData data=new StringData();
		if(list.size()==0){
			expSupplierCata.setInputCode(ChineseCharToEnUtils.getAllFirstLetter(expSupplierCata.getSupplierJc()));
			i=expSupplierCataService.save(expSupplierCata);
			if(i>0){
				data.setCode("success");
				data.setData("保存成功");
			}else{
				data.setCode("error");
				data.setData("保存失败");
			}
		}
		else{
			data.setCode("info");
			data.setData("该厂商已存在，不能重复添加");
		}

		return data;
	}

	/**
	 * 数据删除
	 * @param expSupplierCata
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(ExpSupplierCata expSupplierCata) {
		StringData data=new StringData();
			int i=0;
			i=expSupplierCataService.delete(expSupplierCata);
			if(i>0){
				data.setCode("success");
				data.setData("删除成功");
			}else{
				data.setCode("error");
				data.setData("删除失败");
			}
		return data;
	}

	/**
	 * @param expSupplierCata,q 传递参数
	 * @return List<ExpSupplierCata>    返回类型
	 * @throws
	 * @Title: autoComplete
	 * @Desciption: (消耗品基本信息添加厂商自动补全)
	 * @author CTQ
	 * @date 2017-03-02
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"autoComplete", ""})
	@ResponseBody
	public List<ExpSupplierCata> autoComplete(String q, String limit,ExpSupplierCata expSupplierCata) {
		if(q!=null&&!q.equals("")){
			try {
				q = URLDecoder.decode(q,"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		List<ExpSupplierCata> list = expSupplierCataService.findAutoCompleteList(expSupplierCata,q,limit,expSupplierCata.getSupplierClass());
		return list;
	}

    /**
     * 根据传过来的入(出)库类别返回供货方(收货方)集合
     * @param inoutType 入库 1； 出库 0
     * @param type      类别：如采购、退货(传过来的是对应的代码数字,而不是名称汉字)
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "getSupplierOrStorageList")
    @ResponseBody
    public List getSupplierOrStorageList(@RequestParam("inoutType") String inoutType, @RequestParam("type") String type){
        return expSupplierCataService.getSupplierOrStorageList(inoutType,type);
    }
}